﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Warehousingtes
{
    //委外订单
    public class PurchasingsSrcetenHandler : IRequestHandler<PurchasingsSrcetenCommend, Result>
    {
        private readonly IBaseRepositoryGet<PurchasingsDto> PurchasingsDto;

        public PurchasingsSrcetenHandler(IBaseRepositoryGet<PurchasingsDto> PurchasingsDto)
        {
            this.PurchasingsDto = PurchasingsDto;
        }

        public async Task<Result> Handle(PurchasingsSrcetenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM Purchasings a INNER JOIN Suppliers b on a.SuppliersId=b.SuppliersId INNER JOIN PurchasingType c on c.PurchasingTypeId=c.PurchasingTypeId";
            var list = PurchasingsDto.GetAll(sql, default).ToList();

            var totalcount = list.Count();
            var pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pageSize);
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

            var lief = new
            {
                totalcount,
                pagecount,
                list,
            };

            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = lief;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
